<template>
  <div>
    <h1> 用户中心</h1>

    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="用户姓名">
        <el-input v-model="searchObj.nickname" placeholder="用户姓名"/>
      </el-form-item>

      <el-form-item label="用户账号状态">
        <el-select v-model="searchObj.isDisabled" placeholder="账号状态" >
          <el-option label="启用" value="false"></el-option>
          <el-option label="禁用" value="true"></el-option>
        </el-select>
      </el-form-item>


      <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!--表格-->
    <router-link :to="'/application/usrsave'">
      <el-button type="success" size="mini" icon="el-icon-plus" style="height: 42px;">添加用户信息</el-button>
    </router-link>

    <el-button type="danger" @click="handleBatchDelete">批量删除</el-button>
    <el-table :data="usrList" style="width: 100%" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" width="50">
        <template slot-scope="scope">
          <i class="el-icon"></i>
          <span style="margin-left: 10px">{{ (page - 1) * limit + scope.$index + 1 }}</span>
        </template>
      </el-table-column>


      <el-table-column label="用户头像" width="100" prop="avatar">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" style="width: 80px; height: 80px"/>
        </template>
      </el-table-column>

      <el-table-column label="用户昵称" width="110" prop="nickname">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname}}</span>
        </template>
      </el-table-column>


      <el-table-column label="用户手机" width="110" prop="mobile">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile}}</span>
        </template>
      </el-table-column>


      <el-table-column label="性别" width="100px" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex === 1 ? '女' : '男' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="年龄" width="100" prop="age">
        <template slot-scope="scope">
          <span>{{ scope.row.age }}</span>
        </template>
      </el-table-column>

      <el-table-column label="用户地址" width="200" prop="address">
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="100" prop="isDisabled">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isDisabled"
            @change="handleChangeStatus(scope.row.id, scope.row.isDisabled)"
            active-color="#ff4949"
            inactive-color="#13ce66"
          ></el-switch>
          <span>{{ scope.row.isDisabled ? "禁用" : "启用" }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作">
        <template slot-scope="scope">
          <router-link :to="'/application/useredit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="parseInt(total)"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>

<script>
  import ucenter from "@/api/ucenter";

  export default {
    data() {
      return {
        selectedUsers: [],
        usrList: [],
        searchObj: {},
        page: 1, // 当前页码数，默认为第一页
        limit: 6, // 每页显示的条数，默认为10条数据
        total: 0 // 总记录数，默认为0条数据，后续从后端获取

      }
    },

    created() {
      this.fetchData()
    },

    methods: {
      fetchData(page = 1) {
        this.page = page;
        console.log(JSON.stringify(this.searchObj))
        ucenter.getAllUsrPage(this.page, this.limit, this.searchObj).then(response => {
          this.usrList = response.data.items.records;
          this.total = response.data.items.total;
          console.log(JSON.stringify(this.usrList));

        })

      },

      // 定义方法，用于重置查询条件。
      resetData() {
        this.searchObj = {};
        this.fetchData();
      },

      handleChangeStatus(userId, isDisabled) {
        ucenter.forbiden(userId, isDisabled).then(respone => {
          if (isDisabled == true) {
            this.$message({
              type: 'success',
              message: '账号被禁用'
            });
          } else {
            this.$message({
              type: 'success',
              message: '账号被启用!'
            });
          }
          this.fetchData(this.page);

        }).catch(error => {
          this.$message({
            type: "error",
            message: "修改失败!"
          });
        });

      },

      handleSelectionChange(selection) {
        this.selectedUsers = selection;
      },


      handleBatchDelete() {
        if (this.selectedUsers.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择要删除的记录!'
          })
          return
        }

        this.$confirm("此操作将永久删除这些信息，是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          const selectedIds = this.selectedUsers.map(item => item.id);

          ucenter
            .deleteBrach(selectedIds)
            .then(response => {
              this.fetchData(); // 重新加载数据列表
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            })
            .catch(error => {
              this.$message({
                type: "error",
                message: "删除失败!"
              });
            });
        });
      },

      handleDelete(id) {

        this.$confirm("此操作将永久删除该学生信息，是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          ucenter.deleteUser(id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.fetchData();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      }


    }


  }


</script>


<style>

</style>
